package org.tof.hotel.csv;

import org.apache.log4j.Logger;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class CSVWriter {
    private static CSVWriter instance = null;
    private char delimiter;
    private File file;
    private static Logger logger = Logger.getLogger(CSVWriter.class.getName());

    private CSVWriter() {
    }

    private CSVWriter(String pathname) {
        setPathname(pathname);
        this.delimiter = ',';
    }

    private CSVWriter(String pathname, char delimiter) {
        setPathname(pathname);
        this.delimiter = delimiter;
    }

    public static CSVWriter getInstance() {
        return instance == null ? new CSVWriter() : instance;
    }

    public static CSVWriter getInstance(String pathname) {
        return instance == null ? new CSVWriter(pathname) : instance;
    }

    public static CSVWriter getInstance(String pathname, char delimiter) {
        return instance == null ? new CSVWriter(pathname, delimiter) : instance;
    }

    public String getPathname() {
        return file.getName();
    }

    public void setPathname(String pathname) {
        this.file = new File(pathname);
    }

    public void writeBean(Object javaBean) {
        FileWriter fw = null;
        try {
            
            logger.debug("writing field names for \"" + javaBean.getClass().getName() + "\" to csv file: \"" + getPathname() + "\"");
            fw = new FileWriter(file, false);
            fw.write(((CSVGenerable) javaBean).getFieldNames() + "\r\n");

            logger.debug("writing field values for \"" + javaBean.getClass().getName() + "\" to csv file: \"" + getPathname() + "\"");
            ArrayList<String> fieldValues = new ArrayList<String>(((CSVGenerable) javaBean).getFields());
            for (String fieldValue : fieldValues) {
                fw.write(fieldValue + "\r\n");
            }
            for (String aList : fieldValues) {
                fw.write(aList + "\r\n");
            }

        } catch (FileNotFoundException e) {
            logger.error(e.toString(), e);
        } catch (IOException e) {
            logger.error(e.toString(), e);
        } finally {
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    logger.error(e.toString(), e);
                }
            }
        }
    }

    public String FieldsToCSVString(ArrayList<String> fieldValues) {

        //Concatenate all the field' values and put delimiter between them
        String CSVString = "";
        for (String fieldValue : fieldValues) {
            CSVString = CSVString.concat(fieldValue + String.valueOf(delimiter));
        }

        //Delete the trailing delimiter character
        CSVString = CSVString.replaceFirst(delimiter + "$", "");
        return CSVString;
    }
}
